﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PKTools.Database
{
    public class Transaction<TSql> where TSql : Sql
    {
        public void Block(Action<TSql> executeF, Action<TSql, Exception> exceptionF = null)
        {
            try
            {
                using (var sql = Activator.CreateInstance(typeof(TSql), true) as TSql)
                {
                    executeF(sql);
                    sql.Commit();
                }
            }
            catch (Exception ex)
            {
                if (exceptionF == null)
                    throw;

                using (var sql = Activator.CreateInstance(typeof(TSql), true) as TSql)
                {
                    exceptionF(sql, ex);
                    sql.Commit();
                }
            }
        }
    }
}
